/**
 * 
 */
package util;

import java.io.EOFException;

/**
 * @author LionBule
 */
public class ByteConvert {
	
	public static byte[] int2bytes(int i) {
        byte[] b = new byte[4];

        b[0] = (byte) (0xff&i);
        b[1] = (byte) ((0xff00&i) >> 8);
        b[2] = (byte) ((0xff0000&i) >> 16);
        b[3] = (byte) ((0xff000000&i) >> 24);
        return b;
	}

	public static int bytes2int(byte[] bytes) {
		int num = bytes[0] & 0xFF;
		num |= ((bytes[1] << 8) & 0xFF00);
		num |= ((bytes[2] << 16) & 0xFF0000);
		num |= ((bytes[3] << 24) & 0xFF000000);
		return num;
    }
	
	public static int intArr2int(int[] i) throws EOFException {
		if (i==null || i.length!=4 || (i[0]|i[1]|i[2]|i[3])<0) {
            throw new EOFException();
        }
        return ((i[3]<<24) + (i[2]<<16) + (i[1]<<8) + (i[0]<<0));
	}
	
	public static long bytes2long(byte[] b) {
		int mask = 0xff;
		int temp = 0;
		int res = 0;
		for (int i=0;i<8;i++) {
			res <<= 8;
			temp = b[i] & mask;
			res |= temp;
		}
		return res;
	}

	public static byte[] long2bytes(long num) {
		byte[] b = new byte[8];
		for (int i=0;i<8;i++) {
			b[i] = (byte)(num>>>(56-(i*8)));
		}
		return b;
	}
	
	public static void main(String[] args) {
		
	}

}
